﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla;

namespace Asah.SiteWorx.Commerce
{
    [Serializable()]
    public class OrderInfo : Csla.ReadOnlyBase<OrderInfo>
    {
        private int _id;
        private OrderStatus _status;
        private ShipToInfo _shipToInfo;
        private BillToInfo _billToInfo;
        private PaymentDetailInfo _paymentDetailInfo;
        private List<OrderItemInfo> _orderItemList;
        private Membership.User _createdBy;
        private SmartDate _createdDate;
        private Membership.User _lastUpdatedBy;
        private SmartDate _lastUpdatedDate;

        public int ID
        {
            get
            {
                return _id;
            }
            internal set
            {
                _id = value;
            }
        }

        public OrderStatus Status
        {
            get
            {
                return _status;
            }
            internal set
            {
                _status = value;
            }
        }

        public ShipToInfo ShipToInfo
        {
            get
            {
                return _shipToInfo;
            }
            internal set
            {
                _shipToInfo = value;
            }
        }

        public BillToInfo BillToInfo
        {
            get
            {
                return _billToInfo;
            }
            internal set
            {
                _billToInfo = value;
            }
        }

        public PaymentDetailInfo PaymentDetailInfo
        {
            get
            {
                return _paymentDetailInfo;
            }
            internal set
            {
                _paymentDetailInfo = value;
            }
        }

        public List<OrderItemInfo> OrderItemList
        {
            get
            {
                return _orderItemList;
            }
            internal set
            {
                _orderItemList = value;
            }
        }

        public Membership.User CreatedBy
        {
            get
            {
               return _createdBy;
            }
            internal set
            {
                _createdBy = value;
            }
        }

        public string CreatedDate
        {
            get
            {
                return _createdDate.ToString("g");
            }
            internal set
            {
                _createdDate.Text = value;
            }
        }

        public Membership.User LastUpdatedBy
        {
            get
            {
                return _lastUpdatedBy;
            }
            internal set
            {
                _lastUpdatedBy = value;
            }
        }

        public string LastUpdatedDate
        {
            get
            {
                return _lastUpdatedDate.ToString("g");
            }
            internal set
            {
                _lastUpdatedDate.Text = value;
            }
        }

        public decimal Total
        {
            get
            {
                decimal grandTotal = 0;
                if (_orderItemList != null)
                {
                    foreach (OrderItemInfo item in _orderItemList)
                    {
                        grandTotal += item.Total;
                    }
                }
                             
                return grandTotal;
            }
        }

        public override string ToString()
        {
            return _id.ToString();
        }

        protected override object GetIdValue()
        {
            return _id;
        }

        internal OrderInfo()
        {
            // require use of factory methods
        }

        internal OrderInfo(int id, OrderStatus status, ShipToInfo shipToInfo, BillToInfo billToInfo, 
                           PaymentDetailInfo paymentDetailInfo, Guid createdBy, SmartDate createdDate, 
                           Guid lastUpdatedBy, SmartDate lastUpdatedDate)
        {
            _id = id;
            _status = status;
            _shipToInfo = shipToInfo;
            _billToInfo = billToInfo;
            _paymentDetailInfo = paymentDetailInfo;
            _createdBy = Membership.User.GetUser(createdBy);
            _createdDate = createdDate;
            _lastUpdatedBy = Membership.User.GetUser(lastUpdatedBy);
            _lastUpdatedDate = lastUpdatedDate;
        }
    }
}
